<?php
namespace app\admin\controller\openservice;
use app\common\annotation\NodeAnnotation;

use app\common\controller\Admin;
use app\common\traits\ControllerCurd;
use think\Request;

/**
 * Class Index
 * @NodeAnnotation(
 *     title="开放应用",
 *     parent="admin/openservice.index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1
 * )
 */
class Openapps extends Admin
{
    use ControllerCurd;
    public $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\openservice\Openapps();
    }

    /**
     * @NodeAnnotation(
     *     title="开放应用列表",
     *     parent="admin/openservice.openapps",
     *     MenuRoot="admin/openservice.index"
     * )
     */
    public function index(Request $request)
    {
        $keywords  = $request->param("keywords");

        $where = [];

        if($keywords) array_push($where,['title',"like","%$keywords%"]);

        $this->assign([
            "appList" => $this->model->where($where)->paginate(),
            "search" => [
                "keywords" => $keywords
            ]
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加应用",
     *     parent="admin/openservice.openapps",
     *     MenuRoot="admin/openservice.index",
     *     is_menu = 0
     * )
     */
    public function addapps()
    {
        $this->add(url('admin/openservice.openapps/index'),function($model,$data){
            $model->where("id",$data['id'])->update([
                "app_key" => md5($data['id'])
            ]);
        });
        $this->assign([
            "serviceCategoryList" => (new \app\admin\model\openservice\ServiceCategory())->selectDatas()
        ]);
        return $this->fetch();
    }


    /**
     * @NodeAnnotation(
     *     title="编辑应用",
     *     parent="admin/openservice.openapps",
     *     MenuRoot="admin/openservice.index",
     *     is_menu = 0
     * )
     */
    public function editapps($id=0)
    {
        $this->edit(url('admin/openservice.openapps/index'));
        $this->assign([
            "serviceCategoryList" => (new \app\admin\model\openservice\ServiceCategory())->selectDatas(),
            "app" => $this->model->findData($id)
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除开放应用列表",
     *     parent="admin/openservice.openapps",
     *     MenuRoot="admin/openservice.index",
     *     is_menu = 0
     * )
     */
    public function delapps()
    {
        $this->del();
    }

    /**
     * @NodeAnnotation(
     *     title="设置接口权限",
     *     parent="admin/openservice.openapps",
     *     MenuRoot="admin/openservice.index",
     *     is_menu = 0
     * )
     */
    public function setaccess(Request $request)
    {

        if($request->isAjax() and $request->isPost())
        {
            $input = $request->post();

            $input['rules'] = array_unique($input['rules']);
            $input['rules'] = implode(",",$input['rules']);
            $res = $this->model->update($input);
            if($res)
            {
                $this->success("配置成功");
            }else{
                $this->error("配置失败");
            }
        }

        $id = $request->param("id",0,'intval');

        $role = $this->model->find($id);

        $nodeList = (new \app\admin\model\openservice\OpenServiceApi())->select();

        $role['rules'] = explode(',',$role['rules']);

        foreach ($nodeList as $k => $item)
        {
            if(in_array($item['id'],$role['rules']))
            {
                $nodeList[$k]['checked'] = true;
            }else{
                $nodeList[$k]['checked'] = false;
            }
        }
        return json($nodeList);
    }

}